<?php
/**
 * flight.php
 *
 * Tour类
 * @author konakona
 * @date 2009-10-10
 */
class flight {
	/**
	 * 添加flight
	 * @param string $_post
	*/
	public static function flight_add($_post){
		if(strlen($_post['name'])<2){
			k::message('邮轮不能没有名字呀','javascript:history.go(-1);');
		}
		if($_post['hs_upstream']=='' || $_post['hs_downstream']=='' || $_post['sh_upstream']=='' || $_post['sh_downstream']==''){
			k::message('价格不能为空','javascript:history.go(-1);');
		}
		if($_FILES['exterior_cruise']['name']==''){
			k::message('至少上传一张外部图片作为封面照吧','javascript:history.go(-1);');
		}
		if($_post['itineraries']==''){
			k::message('航线的介绍是不是太少了？','javascript:history.go(-1);');
		}
		if(strlen($_post['informations'])<10){
			k::message('邮轮介绍信息不可以少于10个字符','javascript:history.go(-1);');
		}
		//从旺季里找出其他季
		$moon = array(1,2,3,4,5,6,7,8,9,10,11,12);
		foreach($_POST['high_seasons'] as $key=>$value){
			if(in_array($_POST['high_seasons'][$key],$moon)){
				unset($moon[$value-1]);
			}
		}
		foreach($moon as $value){
			if(!empty($value)){
				$shoulder_seasons.= $value."|";
			}
		}
		//处理旺季月份
		foreach($_post['high_seasons'] as $value){
			if(!empty($value)){
				$high_seasons.= $value."|";
			}
		}
		
		$exterior_cruise = !empty($_FILES['exterior_cruise']['name'][0])?self::movieFile('exterior_cruise',$_FILES):'';
		$interior_cruise = !empty($_FILES['interior_cruise']['name'][0])?self::movieFile('interior_cruise',$_FILES):'';
		$rooms_cruise = !empty($_FILES['rooms_cruise']['name'][0])?self::movieFile('rooms_cruise',$_FILES):'';
		$food_cruise = !empty($_FILES['food_cruise']['name'][0])?self::movieFile('food_cruise',$_FILES):'';
		$deck_plan = !empty($_FILES['deck_plan']['name'][0])?self::movieFile('deck_plan',$_FILES):'';
		$sql = "INSERT INTO flight values('','{$_post[informations]}','{$_post[itineraries]}','{$_post[name]}','{$_post[stars]}','{$_post[hs_upstream]}','{$deck_plan}','{$_post[hs_downstream]}','{$_post[sh_downstream]}','{$high_seasons}','{$shoulder_seasons}','{$exterior_cruise}','{$interior_cruise}','{$rooms_cruise}','{$food_cruise}','{$_post[standard_cabin]}','{$_post[luxury_cabin]}','{$_post[preident_suite]}','{$_post[standard_cabin_area]}','{$_post[luxury_cabin_area]}','{$_post[preident_suite_area]}','{$_post[standard_cabin_bed]}','{$_post[luxury_cabin_bed]}','{$_post[preident_suite_bed]}','{$_post[sh_upstream]}')";

		mysql::connect()->query($sql);
		k::message('经过千辛万苦,游轮入库了！','javascript:history.go(-1);');
	}
	/**
	 * 返回一笔邮轮名
	 * @param string $id
	 * @return stirng
	*/
	public static function getName($id){
		$result = mysql::connect()->fetch_all("select fname from flight where fid = $id");
		return $result[0]['fname'];
	}
	/**
	 * 将数据转移到目录并返回存sql字符串
	 * @param string $name array $_FILES
	 * @return stirng
	*/
	public static function movieFile($name,$_FILES){
		$ImageDir = "pic/";
		if(!$_FILES[$name]['name'][0]==''){
			if(!is_dir($ImageDir))
				k::message('你并没有pic这个文件夹，请建立后再尝试上传','javascript:history.go(-1);');
			$str = '';
			if(isset($_FILES[$name]['tmp_name'][1])){
				foreach($_FILES[$name]['tmp_name'] as $key=>$value){
					$tmp_name = md5($_FILES[$name]['name'][$key].time()).substr($_FILES[$name]['name'][$key],-4,4);
					copy($_FILES[$name]['tmp_name'][$key],$ImageDir.$tmp_name);
					$str.= $tmp_name."|";
				}
			}else{
				if(isset($_FILES[$name]['tmp_name'][0])){
					$tmp_name = md5($_FILES[$name]['name'][0].time()).substr($_FILES[$name]['name'][0],-4,4);
					copy($_FILES[$name]['tmp_name'][0],$ImageDir.$tmp_name);
					$str= $tmp_name."|";
				}else{
					$str = '';
				}
			}
		}
		return $str;
	}
	/**
	 * 删除油輪的同時，清除相關圖片、航班時間、航线。 
	 * @param mixed $id
	*/
	public static function flight_del($id){
		$ImageDir = "pic/";
		$pic = mysql::connect()->fetch_all("select exterior_cruise,interior_cruise,rooms_cruise,food_cruise,deck_plan from flight WHERE fid=$id");
		foreach($pic[0] as $key =>$value){
			$value = explode("|",$value);
			if($value[0]!=''){
				foreach ($value as $v){
					@unlink($ImageDir.$v);//linux主机存在权限问题，请将目录改为777权限
				}
			}
			@mysql::connect()->query("delete from cruise where fid=$id");
		}
                mysql::connect()->query("DELETE FROM cruise WHERE fid = $id");
		mysql::connect()->query("DELETE FROM flight WHERE fid=$id");
		k::message('这条flight已删除成功','javascript:history.go(-1);');
	}
	/**
	 * 列出所有flight
	 * @param int $pid $dis_id
	 * @return array
	*/
	public static function flight_list($pid,$dis_name=false,$row=10){
		if($pid == 0){
			$pid = 1;
		}
		$allcount = page::tableCount('flight');
		if($dis_name==false){
			if($pid==1){
				$sql = "select * from flight limit ".($pid-1).",$row";
			}else{
				$page_start  = ($pid-1)+$row;
				$sql = "select * from flight limit ".($page_start-1).",$row";
			}
		}else{
			if($pid == 1){
				$sql = "select * from flight order by fid DESC limit ".($pid-1).",$row";
			}else{
				$page_start  = ($pid-1)+$row;
				$sql = "select * from flight order by fid DESC limit".($page_start-1).",$row";
			}
		}
		return mysql::connect()->fetch_all($sql);
	}
	/**
	 * 读取一条flight信息
	 * @param int $id
	 * @return array
	*/
	public static function flight_details($id){
		$sql = "select * from flight where fid=$id";
		$result = mysql::connect()->fetch_all($sql);
		if($result[0]==''){
			k::message("Sorry,Can 't find No.{$id} ship.");
		}
		$result[0]['shoulder_seasons'] =  explode("|",$result[0]['shoulder_seasons']);
		array_pop($result[0]['shoulder_seasons'] );
		$result[0]['high_seasons'] =  explode("|",$result[0]['high_seasons']);
		array_pop($result[0]['high_seasons'] );
		$result[0]['exterior_cruise'] =  explode("|",$result[0]['exterior_cruise']);
		array_pop($result[0]['exterior_cruise'] );
		$result[0]['interior_cruise'] =  explode("|",$result[0]['interior_cruise']);
		array_pop($result[0]['interior_cruise'] );
		$result[0]['rooms_cruise'] =  explode("|",$result[0]['rooms_cruise']);
		array_pop($result[0]['rooms_cruise'] );
		$result[0]['food_cruise'] =  explode("|",$result[0]['food_cruise']);
		array_pop($result[0]['food_cruise'] );
		$result[0]['deck_plan'] =  explode("|",$result[0]['deck_plan']);
		array_pop($result[0]['deck_plan'] );
		return $result[0];
	}
	/**
	 * 将数据库里的旺季和其他季月份组成select返回
	 *
	 * @param array $data
	 */
	public static function season_select($data){
		$html = '';
		foreach($data as $key=>$value){
			$data[$value] = $value;
			unset($data[$key]);
		}
		for($i=1;$i<13;$i++){
			if($i==$data[$i]){
				$html .= "<input name='high_seasons[]' type='checkbox' checked value='{$i}'>".($i)."月";
			}else{
				$html .= "<input name='high_seasons[]' type='checkbox' value='{$i}'>".($i)."月";
			}
		}
		return $html;
	}
	/**
	 * 修改一条flight信息
	 * @param int $_post
	*/
	public static function flight_edit($_post){
		if(strlen($_post['name'])<2){
			k::message('邮轮不能没有名字呀','javascript:history.go(-1);');
		}
		if($_post['hs_upstream']=='' || $_post['hs_downstream']=='' || $_post['sh_upstream']=='' || $_post['sh_downstream']==''){
			k::message('价格不能为空','javascript:history.go(-1);');
		}
		if($_post['itineraries']==''){
			k::message('航线的介绍是不是太少了？','javascript:history.go(-1);');
		}
		if(strlen($_post['informations'])<10){
			k::message('邮轮介绍信息不可以少于10个字符','javascript:history.go(-1);');
		}
		//从旺季里找出其他季
		$moon = array(1,2,3,4,5,6,7,8,9,10,11,12);
		foreach($_POST['high_seasons'] as $key=>$value){
			if(in_array($_POST['high_seasons'][$key],$moon)){
				unset($moon[$value-1]);
			}
		}
		foreach($moon as $value){
			if(!empty($value)){
				$shoulder_seasons.= $value."|";
			}
		}
		//处理旺季月份
		foreach($_post['high_seasons'] as $value){
			if(!empty($value)){
				$high_seasons.= $value."|";
			}
		}
		$exterior_cruise = !empty($_FILES['exterior_cruise']['name'][0])?self::movieFile('exterior_cruise',$_FILES):'';
		$interior_cruise = !empty($_FILES['interior_cruise']['name'][0])?self::movieFile('interior_cruise',$_FILES):'';
		$rooms_cruise = !empty($_FILES['rooms_cruise']['name'][0])?self::movieFile('rooms_cruise',$_FILES):'';
		$food_cruise = !empty($_FILES['food_cruise']['name'][0])?self::movieFile('food_cruise',$_FILES):'';
		$deck_plan = !empty($_FILES['deck_plan']['name'][0])?self::movieFile('deck_plan',$_FILES):'';
		$sql = "UPDATE `flight` SET `fname` = '{$_post[name]}',`informations` = '{$_post[informations]}',`itineraries` = '{$_post[itineraries]}',`stars` = '{$_post[stars]}',`hs_upstream` = '{$_post[hs_upstream]}',`deck_plan` = `deck_plan`+'{$deck_plan}',`hs_downstream` = '{$_post[hs_downstream]}',`sh_downstream` = '{$_post[sh_downstream]}',`high_seasons` = '{$high_seasons}',`shoulder_seasons` = '{$shoulder_seasons}',`exterior_cruise` = `exterior_cruise`+'{$exterior_cruise}',`interior_cruise` = `interior_cruise`+'{$interior_cruise}',`rooms_cruise` = `rooms_cruise`+'{$rooms_cruise}',`food_cruise` = `food_cruise`+'{$food_cruise}',`standard_cabin` = '{$_post[standard_cabin]}',`luxury_cabin` = '{$_post[luxury_cabin]}',`preident_suite` = '{$_post[preident_suite]}',`standard_cabin_area` = '{$_post[standard_cabin_area]}',`luxury_cabin_area` = '{$_post[luxury_cabin_area]}',`preident_suite_area` = '{$_post[preident_suite_area]}',`standard_cabin_bed` = '{$_post[standard_cabin_bed]}',`luxury_cabin_bed` = '{$_post[luxury_cabin_bed]}',`preident_suite_bed` = '{$_post[preident_suite_bed]}',`sh_upstream` = '{$_post[sh_upstream]}' where fid={$_post[id]}";
		mysql::connect()->query($sql);
		k::message('修改成功','?action=flight-list');
	}
	/**
	 * 获取指定fid的价格
	 * @param int $id
	*/
	public static function getPrice($id,$month,$way){
		$way = k::escapeSQL(strtolower($way));
		$month = k::escapeSQL($month);
		$way = k::escapeSQL($way);
		$seasons = cruise::getWJ($month,$id);
		$sql = "select `{$seasons[0]}{$way}` as price from flight where `{$seasons[1]}` like '%{$month}%' and fid = {$id}";
		$result = $result = mysql::connect()->fetch_all($sql);
		return $result[0]['price'];
	}
	/**
	 * 通过游轮名获取油轮id
	 * @param string $name
	*/
	public static function getFID($name){
		$result = mysql::connect()->fetch_all("select fid from flight where fname='{$name}'");
		return $result[0]['fid'];
	}
}